package com.jingdianjichi.auth.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.convert.AuthRolePermissionDTOConverter;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.entity.AuthRolePermissionBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.domain.service.AuthRolePermissionDomainService;
import com.jingdianjichi.auth.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.dto.AuthRolePermissionDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Resource
    AuthRolePermissionDomainService authRolePermissionDomainService;

    @RequestMapping("add")
    public Result<Boolean> register(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()),"角色关联权限不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId());
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDtoToBoAuthRolePermission(authRolePermissionDTO);
            return Result.ok(authRolePermissionDomainService.add(authRolePermissionBO));
        } catch (Exception e) {
            return Result.fail("添加权限失败");
        }
    }


}
